package com.qk.management.controller;

import com.qk.common.util.PageResult;
import com.qk.common.util.Result;
import com.qk.entity.Dept;
import com.qk.management.anno.LogOperation;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @GetMapping("/list")
    public Result listDept() {
        log.info("查询所有部门");
        List<Dept> list = deptService.listDept();
        return Result.success(list);
    }
    @DeleteMapping("/{id}")
    @LogOperation
    public Result deleteById(@PathVariable("id") Integer deptid) {
        log.info("删除部门：{}", deptid);
        deptService.deleteById(deptid);
        return Result.success();
    }
    @PutMapping
    @LogOperation
    public Result updateDept(@RequestBody Dept dept) {
        log.info("修改部门：{}", dept);
        deptService.updateDept(dept);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer deptid) {
        log.info("查询部门：{}", deptid);
        Dept dept = deptService.selectById(deptid);
        return Result.success(dept);
    }
    @GetMapping
    public Result page(@RequestParam(name = "name", required = false) String name,
                       @RequestParam(name = "status", required = false) Integer status,
                       @RequestParam(name = "page", defaultValue = "1") Integer page,
                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        PageResult<Dept> pageResult = deptService.page(name, status, page, pageSize);
        log.info("分页查询部门：{}", pageResult);
        return Result.success(pageResult);
    }
    //    @RequestMapping(path="/depts",method=RequestMethod.POST)
    //请求路径：/depts 请求方式：POST
    @PostMapping
    @LogOperation
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门：{}", dept);
        deptService.addDept(dept);
        return Result.success();
    }
}